package com.aidex.biz.controller;

import com.aidex.biz.domain.GongziSetting;
import com.aidex.biz.service.GongziSettingService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工资规则Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/biz/bizGongziSetting")
public class GongziSettingController extends BaseController
{
    @Autowired
    private GongziSettingService gongziSettingService;

    /**
     * 查询工资规则列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizGongziSetting:list')")
    @GetMapping("/list")
    public R<PageInfo> list(GongziSetting gongziSetting, HttpServletRequest request, HttpServletResponse response)
    {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                gongziSetting.setCompanyId(null);
        }else{
                     gongziSetting.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                 }
        gongziSetting.setPage(new PageDomain(request, response));
        return R.data(gongziSettingService.findPage(gongziSetting));
    }

    /**
     * 获取工资规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizGongziSetting:query')")
    @GetMapping(value = "/{id}")
    public R<GongziSetting> detail(@PathVariable("id") String id)
    {
        return R.data(gongziSettingService.get(id));
    }

    /**
     * 新增工资规则
     */
    @PreAuthorize("@ss.hasPermi('biz:bizGongziSetting:add')")
    @Log(title = "工资规则", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  GongziSetting gongziSetting)
    {
        return R.status(gongziSettingService.save(gongziSetting));
    }

    /**
     * 修改工资规则
     */
    @PreAuthorize("@ss.hasPermi('biz:bizGongziSetting:edit')")
    @Log(title = "工资规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated GongziSetting gongziSetting)
    {
        return R.status(gongziSettingService.save(gongziSetting));
    }


    /**
     * 删除工资规则
     */
    @PreAuthorize("@ss.hasPermi('biz:bizGongziSetting:remove')")
    @Log(title = "工资规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(gongziSettingService.deleteGongziSettingByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizGongziSetting:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(gongziSettingService.findMaxSort(new GongziSetting()));
    }

    /**
     * 导出工资规则列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizGongziSetting:export')")
    @Log(title = "工资规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(GongziSetting gongziSetting)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    gongziSetting.setCompanyId(null);
            }else{
                         gongziSetting.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<GongziSetting> list = gongziSettingService.findList(gongziSetting);
        ExcelUtil<GongziSetting> util = new ExcelUtil<GongziSetting>(GongziSetting.class);
        return util.exportExcel(list, "工资规则数据");
    }

}
